package com.ttbj.product.bill.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttbj.api.bill.BillPieceService;
import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.bill.BillCarTypeDto;
import com.ttbj.api.dto.bill.BillDistanceDto;
import com.ttbj.api.dto.bill.BillPieceDto;
import com.ttbj.api.dto.bill.BillSecondDto;
import com.ttbj.api.dto.bill.BillServeDto;
import com.ttbj.api.dto.bill.BillTimingDto;
import com.ttbj.api.dto.bill.BillVehicleDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.bill.bean.po.BillDistance;
import com.ttbj.bill.bean.po.BillPiece;
import com.ttbj.bill.bean.po.BillSecond;
import com.ttbj.bill.bean.po.BillServe;
import com.ttbj.bill.bean.po.BillVehicle;
import com.ttbj.bill.dao.BillPieceMapper;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.constant.SalesmanEnum;
import com.ttbj.constant.annotation.SystemServiceLog;
import com.ttbj.core.util.DtoTransitionUtils;

@Service("billPieceService")
public class BillPieceServiceImpl implements BillPieceService{
	
	@Autowired
	private BillPieceMapper billPieceMapper;

	
	@Override
	public BillPieceDto findBillPieceInfo() throws BusinessException {
		BillPiece billPiece = this.billPieceMapper.queryBillPieceInfo();
		BillPieceDto billPieceDto = (BillPieceDto) DtoTransitionUtils.trans(BillPieceDto.class, billPiece);
		return billPieceDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改按件计费配置")
	public void editBillPieceInfo(BillPieceDto billPieceDto) throws BusinessException {
		if (null == billPieceDto) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		BillPiece billPiece = (BillPiece) DtoTransitionUtils.trans(BillPiece.class, billPieceDto);
		billPiece.setPieceUpdateTime(new Date());//设置修改时间
		int result = this.billPieceMapper.updateBillPieceInfo(billPiece);
		if (result == 0) {
			throw new BusinessException("更改失败，重新更改", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
	}

	@Override
	public BillSecondDto findBillSecondInfo() throws BusinessException {
		BillSecond billSecond = this.billPieceMapper.queryBillSecondInfo();
		BillSecondDto billSecondDto = (BillSecondDto) DtoTransitionUtils.trans(BillSecondDto.class, billSecond);
		return billSecondDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改按次计费配置")
	public void editBillSecondInfo(BillSecondDto billSecondDto) throws BusinessException {
		if (null == billSecondDto) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		BillSecond billSecond = (BillSecond) DtoTransitionUtils.trans(BillSecond.class, billSecondDto);
		billSecond.setSecondUpdateTime(new Date());	//设置修改时间
		int result = this.billPieceMapper.updateBillSecondInfo(billSecond);
		if (result == 0) {
			throw new BusinessException("更改失败，重新更改", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
	}

	@Override
	public List<BillDistanceDto> findBillDistanceInfo() throws BusinessException {
		List<BillDistance> billDistances = this.billPieceMapper.queryBillDistanceInfo();
		List<BillDistanceDto> billDistanceDtos = new ArrayList<>();
		for (BillDistance billDistance2 : billDistances) {
			BillDistanceDto distanceDto = (BillDistanceDto) DtoTransitionUtils.trans(BillDistanceDto.class, billDistance2);
			billDistanceDtos.add(distanceDto);
		}
		return billDistanceDtos;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改里程计费配置")
	public void editBillDistanceInfo(BillDistanceDto billDistanceDto) throws BusinessException {
		if (null == billDistanceDto) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		BillDistance billDistance = (BillDistance) DtoTransitionUtils.trans(BillDistance.class, billDistanceDto);
		billDistance.setDistanceUpateTime(new Date());	//设置修改时间
		int result = this.billPieceMapper.updateBillDistanceInfo(billDistance);
		if (result == 0) {
			throw new BusinessException("更改失败，重新更改", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
	}

	@Override
	public BillServeDto findBillServeInfo() throws BusinessException {
		BillServe billServe = this.billPieceMapper.queryBillServeInfo();
		BillServeDto billServeDto = (BillServeDto) DtoTransitionUtils.trans(BillServeDto.class, billServe);
		return billServeDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改服务权重配置")
	public void editBillServeInfo(BillServeDto billServeDto) throws BusinessException {
		if (null == billServeDto) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		BillServe billServe = (BillServe) DtoTransitionUtils.trans(BillServe.class, billServeDto);
		billServe.setServeUpdateTime(new Date());
		int result = this.billPieceMapper.updateBillServeInfo(billServe);
		if (result == 0) {
			throw new BusinessException("更改失败，重新更改", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
	}

	@Override
	public BillVehicleDto findBillVehicleInfo() throws BusinessException {
		BillVehicle billVehicle = this.billPieceMapper.queryBillVehicleInfo();
		BillVehicleDto billVehicleDto = (BillVehicleDto) DtoTransitionUtils.trans(BillVehicleDto.class, billVehicle);
		return billVehicleDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改车辆权重配置")
	public void editBillVehicleInfo(BillVehicleDto billVehicleDto) throws BusinessException {
		if (null == billVehicleDto) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		BillVehicle billVehicle = (BillVehicle) DtoTransitionUtils.trans(BillVehicle.class, billVehicleDto);
		billVehicle.setVehicleUpdateTime(new Date());
		int result = this.billPieceMapper.updateBillVehicleInfo(billVehicle);
		if (result == 0) {
			throw new BusinessException("更改失败，重新更改", PlatFormErrorCode.DATA_INSERT_ERROR.code());
		}
	}

	@Override
	public List<BillTimingDto> findBillTimingInfo(Page page) throws BusinessException {
		List<BillTimingDto> billTimingDtos = this.billPieceMapper.queryBillTimingInfo(page);
		return billTimingDtos;
	}

	@Override
	public BillTimingDto findBillTimingInfoByType(Integer type) throws BusinessException {
		if (type != 0 && type != 1) {
			throw new BusinessException("参数有误", PlatFormErrorCode.DATA_SECURITY.code());
		}
		BillTimingDto billTimingDto = this.billPieceMapper.queryBillTimingInfoByType(type);
		return billTimingDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改时间配置")
	public void editBillTimingInfo(BillTimingDto billTimingDto) throws BusinessException {
		if (null != billTimingDto) {
			if (billTimingDto.getTimingType() !=0 && billTimingDto.getTimingType() !=1) {
				throw new BusinessException("参数不合法", PlatFormErrorCode.DATA_SECURITY.code());
			}
			billTimingDto.setTimingUpdateTime(new Date());	//设置更新时间
			int result = this.billPieceMapper.updateBillTimingInfo(billTimingDto);
			if (result == 0) {
				throw new BusinessException("修改失败，重新修改", PlatFormErrorCode.DATA_INSERT_ERROR.code());
			}
		}else {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
	}

	@Override
	public List<BillCarTypeDto> findCarTypeInfo(Page page) throws BusinessException {
		List<BillCarTypeDto> billCarTypeDtos = this.billPieceMapper.queryCarTypeInfo(page);
		return billCarTypeDtos;
	}

	@Override
	public BillCarTypeDto findCarTypeInfoById(Integer carTypeId) throws BusinessException {
		if (null == carTypeId) {
			throw new BusinessException("参数有误", PlatFormErrorCode.DATA_SECURITY.code());
		}
		BillCarTypeDto billCarTypeDto = this.billPieceMapper.queryCarTypeInfoById(carTypeId);
		return billCarTypeDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="更改车辆类型及权重")
	public void editCarTypeInfo(BillCarTypeDto billCarTypeDto) throws BusinessException {
		int result = this.billPieceMapper.updateCarTypeInfo(billCarTypeDto);
		if (result == 0) {
			throw new BusinessException("修改失败，重新修改", PlatFormErrorCode.DATA_INSERT_ERROR.code());
		}
	}
	
	@Override
	public void editCarTypeInfoIsDel(BillCarTypeDto billCarTypeDto) throws BusinessException {
		if (billCarTypeDto.getTypeIsDelete() !=SalesmanEnum.DELETE.getCode() 
								&& billCarTypeDto.getTypeIsDelete() !=SalesmanEnum.NOT_DELETE.getCode()) {
			throw new BusinessException("参数有误", PlatFormErrorCode.DATA_SECURITY.code());
		}
		int result = this.billPieceMapper.updateCarTypeInfoIsDel(billCarTypeDto);
		if (result == 0) {
			throw new BusinessException("修改失败，重新修改", PlatFormErrorCode.DATA_INSERT_ERROR.code());
		}
	}
}
